package com.gxgeek.routing;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.time.LocalDate;
import java.util.concurrent.TimeoutException;

/**
 * @author gaoxing
 * @create 2018-05-17 上午12:38
 */
public class EmitLogDirect {
    private static final String EXCHANGE_NAME = "direct_logs";

    public static void main(String[] argv)
            throws java.io.IOException, TimeoutException {

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);



        //消息内容1 info类型
        String message = "Hello World!";
        channel.basicPublish(EXCHANGE_NAME, "info",null, message.getBytes());
        System.out.println("[product] Send '"+ message +"'");

        //消息内容2 error类型
        message = "Has Error!";
        channel.basicPublish(EXCHANGE_NAME, "error",null, message.getBytes());
        System.out.println("[product] Send '"+ message +"'");

        //消息内容3 warning类型
        message = "Has Warning!";
        channel.basicPublish(EXCHANGE_NAME, "warning",null, message.getBytes());
        System.out.println("[product] Send '"+ message +"'");


        channel.close();
        connection.close();
    }

    private static String getSeverity(String[] argv) {
        return "";
    }
    //..


    private static String getMessage(String[] strings){
        if (strings.length < 1)
            return "info: Hello World!";
        return joinStrings(strings, " ");
    }

    private static String joinStrings(String[] strings, String delimiter) {
        int length = strings.length;
        if (length == 0) return "";
        StringBuilder words = new StringBuilder(strings[0]);
        for (int i = 1; i < length; i++) {
            words.append(delimiter).append(strings[i]);
        }
        return words.toString();
    }

}
